package com.zxw.article.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxw.article.service.LinkService;
import com.zxw.common.api.ApiResult;
import com.zxw.common.api.PageResult;
import com.zxw.common.constant.ApiConst;
import com.zxw.common.entity.link.LinkEntity;
import com.zxw.common.entity.link.LinkListQuery;
import com.zxw.common.entity.link.LinkQuery;
import com.zxw.common.entity.link.LinkVO;
import com.zxw.common.enums.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 友情链接接口
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-09-20
 */
@Api(tags = "友情链接接口", description = "LinkController")
@RestController
@RequestMapping(ApiConst.API_PREFIX +"/link")
public class LinkController {

    @Autowired
    private LinkService linkService;

    @ApiOperation("添加")
    @PostMapping("/save")
    public ApiResult<ResultCode> save(@RequestBody LinkQuery linkQuery) {
        linkService.save(linkQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("列表条件分页查询")
    @GetMapping("/pageList")
    public ApiResult<PageResult<LinkVO>> pageList(LinkListQuery linkListQuery) {
        IPage<LinkVO> iPage = linkService.pageList(linkListQuery);
        return ApiResult.success(PageResult.restPage(iPage, iPage.getRecords()));
    }

    @ApiOperation("列表查询")
    @GetMapping("/queryList")
    public ApiResult<List<LinkVO>> queryList() {
        List<LinkVO> list = linkService.queryList();
        return ApiResult.success(list);
    }

    @ApiOperation("批量添加")
    @PostMapping("/saveBatch")
    public ApiResult<ResultCode> saveBatch(@RequestBody List<LinkQuery> list) {
        linkService.saveBatch(list);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public ApiResult<ResultCode> update(@RequestBody LinkQuery linkQuery) {
        linkService.updateById(linkQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("批量删除")
    @PostMapping("/deletes")
    public ApiResult<ResultCode> deletes(@RequestBody List<String> ids) {
        linkService.removeByIds(ids);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public ApiResult<LinkVO> detail(@PathVariable("id") String id) {
        LinkEntity linkEntity = linkService.getById(id);
        LinkVO linkVO = new LinkVO();
        BeanUtils.copyProperties(linkEntity, linkVO);
        return ApiResult.success(linkVO);
    }

}

